package com.example.jwtauthdemo.controller;

import com.example.jwtauthdemo.entity.Homework;
import com.example.jwtauthdemo.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/homeworks")
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;

    /**
     * 获取所有作业列表（包含提交人数统计）
     */
    @GetMapping( "/getAllHomeworks")
    public ResponseEntity<List<Homework>> getAllHomeworks() {
        try {
            List<Homework> homeworks = homeworkService.getAllHomeworksWithSubmitCount();
            return ResponseEntity.ok(homeworks);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取作业统计数据
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Integer>> getHomeworkStats() {
        try {
            Map<String, Integer> stats = new HashMap<>();
            stats.put("total", homeworkService.countAllHomeworks());
            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 发布新作业（设置默认classId和teacherId）
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createHomework(@RequestBody Homework homework) {
        Map<String, Object> response = new HashMap<>();
        try {
            System.out.println("接收到作业数据: " + homework); // 添加日志输出

            // 设置创建时间如果为空
            if (homework.getCreateTime() == null) {
                homework.setCreateTime(new java.util.Date());
            }

            // 设置默认值
            if (homework.getClassId() == null) {
                homework.setClassId("464786"); // 默认classId
            }

            if (homework.getTeacherId() == null) {
                homework.setTeacherId("2025001"); // 默认teacherId
            }

            // 添加作业
            int result = homeworkService.addHomework(homework);

            response.put("success", true);
            response.put("data", result);
            response.put("message", "作业发布成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("message", "发布作业失败: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 获取作业详情
     */
    @GetMapping("/{homeworkId}")
    public ResponseEntity<Homework> getHomeworkDetail(@PathVariable Long homeworkId) {
        try {
            Homework homework = homeworkService.getHomeworkById(homeworkId);
            if (homework != null) {
                return ResponseEntity.ok(homework);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }
}
